/*
based on original source by tachyon at:

http://www.perlmonks.org/?node_id=344087

*/


// read_big_file.cpp : Defines the entry point for the console application.



#include "stdafx.h"

#include <stdio.h>
#include <string.h>

#define FILENAME "C:\\torbjoernscode\\visualiseringbang\\resources\\weir40.dat"
#define CHUNK 512
#define MAX_LINE_LENGTH 2000

/*
Flow3D text format
xyz always + up to 5 floats for additional data (velocities u,v,w, temp, pressure, fraction-of-fluid)

Data may often be sparse. Lots of zeros.

 printing f, u, v and w       t=0.0  ix=3 to  62   jy=2 to  21  kz=3 to  36 
    1    1    0.000E+00    0.000E+00    3   62    2   21    3   36
       x              y              z             f              u              v              w                     
 -9.7500000E+00  2.5000000E-01  2.5000000E-01  1.0000000E+00  2.0000000E+01  0.0000000E+00  0.0000000E+00

*/

int _tmain(int argc, _TCHAR* argv[])
{
		printf("START!\n");
    FILE *file_handle;
		// Two buffers that we swap between
    char buf1[CHUNK];
		char buf2[CHUNK];

		// Two pointers
    char *frontbuf = 0x0;
    char *backbuf = 0x0;
		
		char lineBuf[MAX_LINE_LENGTH];
		int line_buf_pos = 0;
		
		for(int i=0; i<MAX_LINE_LENGTH; i++) lineBuf[i] = 0;
    
    int r, i;

    file_handle=fopen(FILENAME,"r");
    if (!file_handle)
        return 1;

		// Tell us filesize
    fseek(file_handle,0,SEEK_END);
    int size = ftell(file_handle);
    printf("%i MB\n",size / 1024 / 1024 );
		int total_num_iters = size / CHUNK;
		printf("%i iters to complete fileread\n",total_num_iters );
		
		// Go back to beginning
		fseek(file_handle,0,SEEK_SET);

		int iter_percent = total_num_iters / 100;

    frontbuf=buf1;
    backbuf=buf2;
		bool front_buf_active = true;

		// see if a chunk can be read
    r=(int)fread( frontbuf, sizeof(char), CHUNK, file_handle );
    if ( !r || r<CHUNK )
        return 1;
        
    printf("r=%i\n",r);

		int iters = 0;
		int percent_complete = 0;
		
		int frame_pos = 0;
		
		// Read a chunk and parse it
    while ( (r=(int)fread( backbuf, sizeof(char), CHUNK, file_handle )) ) {
        for( i=0;i<r;i++ ) {

						char current = 'a';

						current = backbuf[i];
						
						// Accumulate chars into a line
						if(line_buf_pos < MAX_LINE_LENGTH)
						{
							lineBuf[line_buf_pos] = current;
							line_buf_pos++;
						}
						
						// Clear the line buffer when we hit a newline
						if(current=='\n')
						{
							char str[] = "printing";
							char *pchar = strstr(lineBuf, "printing");
							if(pchar)
							{
								frame_pos++;
								//printf("%s\n", &lineBuf[0] );
							}

							//
							line_buf_pos = 0;	
						}

        }
        /* Move old back buffer pointer to front buffer ptr
         * And vice versa. Net effect is to slide buffer->R
         * As we will refil the back buffer with fresh data.
         * Thus we simply pour data from disk to memory with
         * no wasted copying effort.
         */
        char *swap=frontbuf;
        frontbuf=backbuf;
        backbuf=swap;
				front_buf_active = !front_buf_active;

				if( iters % iter_percent == 0)
				{
					printf("%i percent \n",percent_complete);
					percent_complete++;
				}
				iters++;
    }
		printf("100% !");

    fclose(file_handle);
    return 0;
}

